<?php
/**
 * 领取奖品核销
 * User: cxm
 * Date: 2020/9/29
 * Time: 13:40
 */
namespace App\Http\Controllers;
use App\Models\AwardHistory;
use Carbon\Carbon;
use DB;
class DrawController extends Controller {
    /**
     * 奖品核销
     * URL ANY:/draw
     * @return mixed
     */
    public function draw(){
        // 活动ID
        $activity_id = request()->get('activity_id');
        // 用户ID
        $user_id = request()->get('user_id');
        // 奖品ID
        $award_id = request()->get('award_id');
        if (empty($activity_id) || empty($user_id) || empty($award_id)) {
            return $this->error('无效二维码');
        }
        // 查看用户是否有未领取的奖品
        $where = [];
        $where[] = ['user_id',$user_id];
        $where[] = ['activity_id',$activity_id];
        $where[] = ['award_id',$award_id];
        $history = AwardHistory::where($where)->first();
        if (!$history) {
            return $this->error('没有领取的奖品');
        }
        if ($history->status == 1) {
            return $this->error('您已经兑奖');
        }
        $field = [
            'draw_time'         => Carbon::now(),
            'status'            => 1
        ];
        $result = AwardHistory::modify($where,$field);
        if ($result === false) {
            return $this->error('领取失败');
        }
        return $this->success('领取成功');
    }
}